/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.CI.Profile;

import entity.Employee;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import managedbean.Controller;
import session.stateless.EmployeeBeanLocal;

/**
 *
 * @author Vincent
 */
@ManagedBean(name="viewGlobalListManagerBean")
@ViewScoped
public class ViewGlobalListManagerBean implements Serializable{
    @EJB 
    private EmployeeBeanLocal employeeBeanLocal; 
    private Employee selectedEmployee;
    private Employee employee;
    private String statusMessage;
    /** Creates a new instance of PositionDataTableBean */
    
    public ViewGlobalListManagerBean() {
    }

    public List<Employee> getEmployees() { 
        return employeeBeanLocal.getEmployees(); 
    }

    public Employee getSelectedEmployee() {
        return selectedEmployee;
    }
    public void setSelectedEmployee(Employee selectedEmployee) {
        this.selectedEmployee = selectedEmployee;
    }
    
    public void addContactList(ActionEvent event){
        
        boolean check = false;
        for(Employee e:this.employee.getContactList()){
            if (e.getEmployeeId() == selectedEmployee.getEmployeeId())
                check = true;
        }
        if (!check){
            this.employee.getContactList().add(selectedEmployee);
            employeeBeanLocal.addContactList(employee.getEmployeeId(), selectedEmployee.getEmployeeId());
            statusMessage = "Save employee " + selectedEmployee.getEmployeeName() + " to contact list successfully"; 
 
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Add Contact List Result: " +statusMessage + "", "")); 
        }
    }
    
    public void initView(PhaseEvent event){
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        this.employee = controller.getEmployee();
    }
}
